$(function() {
  var listData = {
    "initData": { //默认参数配置
      "page": "1",
      "pageSize": "10"
    },
    "url": site.apiUrl + "/doctorManager/selectMedicalDoctorInfoPage", // 数据请求url
    "type": "get",
    // "checkname": "datachk",
    "col": [ //数据列
      { "field": "title", "display": "标题" },
      { "field": "url", "display": "视频地址" },
      {
        "field": "status",
        "display": "状态",
        "width": "1",
        "show": [
          { "value": "0", "html": "<span class='red'>无效</span>" },
          { "value": "1", "html": "<span class='green'>有效</span>" }
        ]
      },
      { "field": "createTime", "display": "时间", "fun": "changeDate", "width": "2" }
    ],
    "option": [ // 操作按钮配置 icon: add, repartyorg, ogoto, edit, more, senior, knotitem, del
      { "icon": "repartyorg", "tip": "审核", "url": "view.html?id={{id}}" },
      { "icon": "ogoto", "tip": "使用", "url": "edit.html?id={{id}}&type=2" },
      { "icon": "edit", "tip": "编辑", "url": "edit.html?id={{id}}&type=2" },
      { "icon": "del", "tip": "删除", "fun": "deleteBrand('{{id}}')" }
    ]
  }
  $("#propagandaVedioList").getList(listData);

  //批量删除
  $("#batchDelBtn").click(function() {
    var c = "";
    $("input[name=datachk]:checked").each(function() {
      c += $(this).val() + ',';
    });
    if (c != null && c != '') {
      deleteBrand(c);
    } else {
      ly.toast("请选择删除项");
    }
  });

  $("body").on("click", "#export_excel", function() {
    exportExcelWithJS();
  })
});

// 导出EXCEL
function exportExcelWithJS(){
  // 表格数据是否为空
  var tableIsEmpty = $('#propagandaVedioList table tr td').hasClass("nodata");
  if (!tableIsEmpty) {
    ly.confirm("确定导出吗？", function(r) {
      $('#propagandaVedioList table').tableExport({
        type:'excel',
        fileName:'医生列表',
      });
    })
  } else {
    ly.toast("数据为空")
  }
}

// 设置默认图片
function changePic(v, o, d) {
  if (d.doctorImg != null && d.doctorImg != '') {
    $(o).html("<img src=" + d.doctorImg + " width='30' height='30'>");
  } else {
    $(o).html("<img src='" + site.interUrl + "/img/default_head.jpg' width='30' height='30'>");
  }
}

// 时间戳转换时间
function changeDate(v, o, d) {
  var baseDateTime;
  var timestamp = new Date(d.createTime);
  baseDateTime = timestamp.toLocaleDateString().replace(/\//g, "-") + " " + timestamp.toTimeString().substr(0, 8)
  if (d.createTime != null && d.createTime != '') {
    $(o).html(baseDateTime)
  }
}

//删除
function deleteBrand(id) {
  ly.confirm("确定删除吗？", function(r) {
    var url = site.apiUrl + "/doctorManager/deleteMedicalPopular";
    request({
      url: url,
      data: { "id": id },
      type: "post",
      callback: function(f) {
        if (f.code == 200) {
          ly.toast("删除成功");
          $("#propagandaVedioList").getList.refresh(); //刷新当前分页
        } else {
          ly.toast(f.desc || f.msg);
        }
      }
    })
  })
}